// This script will export a "grimbo.createBookmarkletList" function that can
// generate bookmarklet <a> tags from a list of script URLs.
(function() {
    var head = document.getElementsByTagName("head")[0];

    function escapeHTML(s) {
        return s.split('&').join('&amp;').split('<').join('&lt;').split('"').join('&quot;');
    }

    function qualifyURL(url) {
        var el = document.createElement('div');
        el.innerHTML= '<a href="' + escapeHTML(url) + '">x</a>';
        return el.firstChild.href;
    }

    /**
     * Gets the src attribute of the last <script> in <head>.
     */
    function getThisScriptSrc() {
        var scripts = head.getElementsByTagName("script");
        if (!scripts || scripts.length < 1) {
            return null;
        }
        var script = scripts[scripts.length - 1];
        var src = script.src;
        // Necessary for IE9 which might only return a relative path.
        src = qualifyURL(src);
        return src;
    }

    /**
     * @return The path to the script. E.g.
     *         "http://localhost/bookmarklets/common/" is returned for
     *         "http://localhost/bookmarklets/common/js-loader.js?js=../infoq/infoq.js".
     */
    function getRoot(scriptSrc) {
        var parts = scriptSrc.split("?");
        parts = parts[0].split("/");
        parts = parts.splice(0, parts.length - 1);
        var src = parts.join("/") + "/";
        return src;
    }

    var src = getThisScriptSrc();
    var root = getRoot(src);

    function BookmarkletHelper(opts) {
        opts = opts || {};
        opts.requireJsSrc = opts.requireJsSrc || "https://rawgithub.com/jrburke/requirejs/2.1.8/require.js";
        opts.root = opts.root || root;
        this.opts = opts;
    }

    BookmarkletHelper.prototype.createRequireJSBookmarkletAnchor = function(info) {
        var opts = this.opts;
        var dataMain = opts.root + info.path;
        var href = "javascript:(function(){\
            var s = document.createElement('script');\
            s.src='" + opts.requireJsSrc + "';\
            s.setAttribute('data-main', '" + dataMain + "');\
            document.getElementsByTagName('head')[0].appendChild(s);\
            })();";
        var linkText = info.title || info.path || '';
        var spanHtml = '<a href="' + href + '">' + linkText + '</a>\
            &nbsp;[' + info.path + ']';
        var span = document.createElement("span");
        span.innerHTML = spanHtml;
        return span;
    }

    BookmarkletHelper.prototype.createBookmarkletAnchor = function(bookmarklet) {
        var opts = this.opts;
        if (bookmarklet.path) {
            return this.createRequireJSBookmarkletAnchor(bookmarklet);
        }
        // js-loader.js HAS to be in same folder as this file (list.js)
        var src = opts.root + "/js-loader.js?js=" + bookmarklet;
        var href = "javascript:(function(){var s = document.createElement('script');s.src='" + src + "'; document.getElementsByTagName('head')[0].appendChild(s);})();";
        var a = document.createElement("a");
        a.setAttribute("href", href);
        a.innerHTML = bookmarklet;
        return a;
    }

    BookmarkletHelper.prototype.createBookmarkletList = function(bookmarklets) {
        var ul = document.createElement("ul");
        for (var i = 0; i < bookmarklets.length; i++) {
            var a = this.createBookmarkletAnchor(bookmarklets[i]);
            var li = document.createElement("li");
            li.appendChild(a);
            ul.appendChild(li);
        }
        return ul;
    }

    var grimbo = window['grimbo'] = window['grimbo'] || {};
    grimbo.BookmarkletHelper = BookmarkletHelper;

}());